<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/**
 * @category   Zead
 * @package    Zead_Manifest
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License 
 */
class Zead_Manifest_Node implements RecursiveIterator, Countable
{	
	/** @var array **/
	protected $_attrs = array();
	
	/** @var array **/
	protected $_attrList = array();
	
	/** @var array **/
	protected $_childNodes = array();
	
	/** @var int **/
	protected $_position = 0;

	/** @var Zead_Manifest_Node **/
	protected $_parentNode = null;
	
	
	public function current()
	{
		return $this->_childNodes[$this->_position];
	}
    
	public function key()
	{
		return $this->_position;
	}
	
	public function next()
	{
		$this->_position++;
		return $this;
	}
	
	public function rewind()
	{
		$this->_position = 0;
		return $this;
	}
	
	public function valid()
	{
		return (isset($this->_childNodes[$this->_position]));
	}
	
	public function count()
	{
		return count($this->_childNodes);
	}
	
	public function hasChildren()
	{
		return (bool) count($this->_childNodes);
	}
	
	public function getChildren()
	{
		return $this->_childNodes;
	}
	
	public function append(Zead_Manifest_Node $node)
	{
		if ($node === $this) {
			throw new Exception('Not is possible append the node to herself');
		}
		$this->_childNodes[] = $node;
	}
	
	public function __get($attr)
	{
		return $this->_attrs[$attr];
	}
	
	public function __set($attr, $value)
	{
		if (count($this->_attrList) && in_array($attr, $this->_attrList)) {
			$this->_attrs[$attr] = $value;
		}
	}
	
	public function getParent()
	{
		return $this->_parentNode;
	}
	
	public function hasParent()
	{
		return null === $this->_parentNode ? true : false;
	}
	
	public function setParent(Zead_Manifest_Node $parentNode)
	{
		$this->_parentNode = $parentNode;
	}
	
	public function getAttr($attr)
	{
		return $this->__get($attr);
	}
}